package dao

import (
	"booksPro/model"
	"booksPro/utils"
)

// 验证用户名和密码  ,根据用户名和密码从数据库中查询数据
func CheckUserAndPassword(username string, password string) (*model.User, error) {
	//通过用户名和密码从数据库里获取到user的所有信息
	sqlStr := "select id,username,password,email from users where username =? and password=?"
	row := utils.Db.QueryRow(sqlStr, username, password)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.Email)
	return user, nil
}

// 验证用户名 有没有重复， 可不可用
func CheckUsername(username string) *model.User {
	sqlStr := "select id,username,password,email from users where username = ?"
	row := utils.Db.QueryRow(sqlStr, username)
	user := &model.User{}
	row.Scan(&user.ID, &user.Username, &user.Password, &user.Email)
	return user
}

// 保存用户信息
func SaveUser(username string, password string, email string) error {
	sqlStr := "insert into users(username,password,email) values(?,?,?)"
	_, err := utils.Db.Exec(sqlStr, username, password, email)
	if err != nil {
		return err
	}
	return nil
}
